import 'package:audioplayers/audioplayers.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:lyps_music/components/home_control.dart';
import 'package:lyps_music/components/left_panel.dart';
import 'package:lyps_music/components/top_bar.dart';
import 'package:lyps_music/request/index.dart';
import 'package:lyps_music/router/index.dart';
import 'package:lyps_music/store/index.dart';
import 'package:provider/provider.dart';
import 'package:window_manager/window_manager.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await windowManager.ensureInitialized();
  var windowOptions = const WindowOptions(size: Size(1000, 600), center: true, titleBarStyle: TitleBarStyle.hidden);
  windowManager.waitUntilReadyToShow(windowOptions, () async {
    await windowManager.show();
    await windowManager.focus();
  });
  MyRequest.init();
  runApp(ChangeNotifierProvider(
    create: (_) => Store(),
    child: MyApp(appRouter: AppRouter()),
  ));
}

class MyApp extends StatelessWidget {
  final AppRouter appRouter;
  const MyApp({super.key, required this.appRouter});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'lyps动感音乐',
      routerConfig: appRouter.config(),
    );
  }
}

@RoutePage()
class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/images/background/background.jpg"),
            fit: BoxFit.cover,
          ),
        ),
        child: Container(
          color: Colors.black.withOpacity(0.4),
          child: Row(
            children: [
              const LeftPanel(),
              Expanded(
                child: Column(
                  children: [
                    const TopBar(),
                    Expanded(
                      child: Column(
                        children: [
                          const Expanded(
                            child: AutoRouter(),
                          ),
                          Selector<Store, dynamic>(
                            selector: (p0, p1) => p1.currentMusic,
                            builder: (context, value, child) {
                              if (value != null) {
                                return Selector<Store, AudioPlayer>(
                                  selector: (p0, p1) => p1.audioplayer,
                                  builder: (context, value2, child) {
                                    if (value2 != null) return const HomeControl();
                                    return Container();
                                  },
                                );
                              }
                              return Container();
                            },
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
